package cn.net.cfss.fgbp.provider.console.biz;

import cn.net.cfss.fgbp.api.console.dto.SysRoleMenuDto;
import cn.net.cfss.fgbp.api.console.vo.SysRoleMenuVo;
import cn.net.cfss.fgbp.base.common.biz.BaseBiz;
import cn.net.cfss.fgbp.provider.console.dao.SysRoleMenuDtoMapper;
import cn.net.cfss.fgbp.provider.console.utils.FgbpIdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author oygq
 * @title: RoleMenuDtoBiz
 * @description: TODO
 * @date 2019/4/2 11:55
 */
@Slf4j
@Service
public class SysRoleMenuDtoBiz extends BaseBiz<SysRoleMenuDtoMapper, SysRoleMenuDto> {

    @Autowired
    SysRedisCacheBiz redisCacheBiz;

    @Transactional
    public void deleteBatch(Long[] roleMenuIds){
        for(Long id : roleMenuIds){
            deleteById(Long.valueOf(id));
        }
    }

    @Transactional
    public void insertBatch(SysRoleMenuVo vo){
        for (String s : vo.getMenuIds()){
            SysRoleMenuVo roleMenuVo = new SysRoleMenuVo();
            roleMenuVo.setId(FgbpIdUtil.generateId());
            roleMenuVo.setMenuId(Long.valueOf(s));
            roleMenuVo.setRoleId(vo.getRoleId());
            insert(roleMenuVo);
        }
    }

    public void  deleteByMenus(SysRoleMenuVo vo){
        mapper.deleteByMenus(vo);
    }

    public List<String> queryRoleAuth(Long roleId){
        return mapper.queryRoleAuth(roleId);

    }

    /**
     * 角色权限缓存
     * @param roleId
     */
    public void pushRoleCache(Long roleId){
        if(null == roleId){
            log.error("角色id不能为null");
            return;
        }
        Map<Long, List<String>> authMap = new HashMap<>();
        authMap.put(roleId, queryRoleAuth(roleId));
        try {
            redisCacheBiz.roleAuthModify(authMap);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("角色权限缓存Redis错误:{},角色id:{}", e.getMessage(), roleId);
        }
    }

}
